package com.dataextend.controller;

import com.dataextend.entity.*;
import com.dataextend.service.*;
import com.dataextend.utils.TreeNodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(value = "权限表接口")
@RestController
@RequestMapping("/api/permission")
@CrossOrigin(origins={"http://localhost:8080"},allowCredentials="true")
public class RootPermissionController extends RootBaseApiController {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePermissionService rolePermissionService;

    @ApiOperation("添加权限")
    @RequiresPermissions("permission:add")
    @PostMapping("/add")
    public Map<String, Object> add(String url,
                                   String name,
                                   Long parentId)
    {
        PermissionEntity permissionEntity = new PermissionEntity()
                .setUrl(url)
                .setName(name)
                .setParentId(parentId);

        if (permissionService.add(permissionEntity)==0) {
            return resp(406,"添加失败");
        } else {
            return resp(200, "添加成功", permissionEntity);
        }
    }

    @ApiOperation("修改权限信息")
    @RequiresPermissions("permission:update")
    @PostMapping("/update")
    public Map<String, Object> update(Long id,
                                      String url,
                                      String name,
                                      Long parentId) {
        PermissionEntity permissionEntity = new PermissionEntity()
                .setId(id)
                .setUrl(url)
                .setName(name)
                .setParentId(parentId);

        return resp(200, "修改成功", permissionService.update(permissionEntity));
    }

    @ApiOperation("删除权限")
    @RequiresPermissions("permission:delete")
    @PostMapping("/delete")
    public Map<String, Object> deleteById(Long id) {
        /*
         * 先查询是不是父节点id
         * 如果是父节点id
         * 查询父级id下的所有子节点id
         * for循环删除所有子节点id
         * 根据子节点id，去用户权限表删除所有与此节点id有关信息
         * */
        PermissionEntity permissionEntity = permissionService.queryById(id);
        if (permissionEntity.getParentId() == 0) {
            List<PermissionEntity> list = permissionService.list(new PermissionEntity().setParentId(id));
            for (PermissionEntity p: list) {
                permissionService.delete(p.getId());
                List<RolePermissionEntity> list1 = rolePermissionService.list(new RolePermissionEntity().setPermissionId(p.getId()));
                for (RolePermissionEntity rp: list1) {
                    rolePermissionService.delete(rp.getId());
                }
            }
        }

        /*
         * 判断在用户权限表内是否有相关信息
         * 不是父节点id，只需要删除该节点id
         * 然后去角色权限表删除所有与此节点id有关信息
         * */
        List<RolePermissionEntity> list = rolePermissionService.list(new RolePermissionEntity().setPermissionId(id));
        if (list.size() != 0) {
            for (RolePermissionEntity rp: list) {
                rolePermissionService.delete(rp.getId());
            }
        }
        permissionService.delete(id);
        return resp(200, "删除成功");
    }

    @ApiOperation("根据id查询权限信息")
    @GetMapping("/query")
    public Map<String, Object> queryById(Long id) {
        return resp(200, "查询成功", permissionService.queryById(id));
    }

    @ApiOperation("查询所有权限信息")
    @GetMapping("/list")
    public Map<String, Object> list(Long id,
                                    String url,
                                    String name,
                                    Long parentId) {
        PermissionEntity permissionEntity = new PermissionEntity()
                .setId(id)
                .setUrl(url)
                .setName(name)
                .setParentId(parentId);

        return resp(200, "查询所有成功", permissionService.list(permissionEntity));
    }

    @ApiOperation("查询所有权限树信息")
    @RequiresPermissions("permission:list:menu")
    @GetMapping("/list/menu")
    public Map<String, Object> listMenu() {
        TreeNodeUtils treeNodeUtils = new TreeNodeUtils();
        // 定义一个总集合
        List<Object> menusList = new ArrayList<>();

        // 查询第一层数据，然后遍历
        List<PermissionEntity> menusListOne = permissionService.list(new PermissionEntity().setParentId(0L));
        for (PermissionEntity m1: menusListOne) {
            // 根据第一层的父级id查询第二层的节点
            List<PermissionEntity> menusListTwo = permissionService.list(new PermissionEntity().setParentId(m1.getId()));
            // 判断，如果第二层的集合不为空说明有节点，把它取出来，添加到父级节点下面
            if (menusListTwo.size() != 0) {
                // 定义一个集合，用来存储第二层的集合
                List<Object> menusListTwoData = new ArrayList<>();
                // 循环添加节点
                for (PermissionEntity m2 : menusListTwo) {
                    menusListTwoData.add(treeNodeUtils.tree(m2.getId(), m2.getName(), m2.getParentId(), ""));
                }
                // 然后再添加到父节点
                menusList.add(treeNodeUtils.tree(m1.getId(), m1.getName(), m1.getParentId(), menusListTwoData));
            } else {
                menusList.add(treeNodeUtils.tree(m1.getId(), m1.getName(), m1.getParentId(), ""));
            }
        }

        return resp(200, "查询所有成功", menusList);
    }
}
